package ui.history;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import blservice.history.SystemLogBLService;
import tool.DateChooserJButton;
import tool.MyButton;
import tool.MyLabel;
import vo.SystemLogVo;
import vo.UserVo;

import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;

import client.boot.BLServiceManager;

public class SystemLogPanel extends JPanel{
	private static final long serialVersionUID = -9016884218956444023L;

	@SuppressWarnings("unused")
	private UserVo user;
	
	private SystemLogBLService bl;
	private JTable table;
	private List<SystemLogVo> list;
	private DateChooserJButton timeFrom;
	private DateChooserJButton timeTo;

	public SystemLogPanel(UserVo user) {
		try {
			bl=BLServiceManager.getSystemLogBLService();
		} catch (RemoteException | MalformedURLException | NotBoundException
				| SQLException e1) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n"+e1.getMessage());
			e1.printStackTrace();
		}
		setLayout(null);
		this.setOpaque(false);
		setSize(646, 465);
		JLabel lblNewLabel = new MyLabel("起始时间");
		lblNewLabel.setBounds(255, 10, 65, 15);
		add(lblNewLabel);

		timeFrom = new DateChooserJButton();
		timeFrom.setBounds(323, 8, 78, 19);
		add(timeFrom);

		JLabel label = new MyLabel("\u7ED3\u675F\u65F6\u95F4");
		label.setBounds(453, 10, 70, 15);
		add(label);

		timeTo = new DateChooserJButton();
		timeTo.setBounds(518, 8, 78, 19);
		add(timeTo);

		JButton selectButton = new MyButton("Graphics/Button65-23.png");
		selectButton.setText("筛选");
		selectButton.addActionListener(e->select());
		selectButton.setBounds(533, 35, 65, 23);
		add(selectButton);

		table = new JTable();
		JScrollPane jsp=new JScrollPane(table);
		jsp.setBounds(50, 68, 548, 315);
		add(jsp);
		
		JButton exportButton = new MyButton("Graphics/Button93-23.png");
		exportButton.setText("导出");
		exportButton.addActionListener(new tool.JTableExportListener(table, "销售情况表", ""));
		exportButton.setBounds(503, 393, 93, 23);
		add(exportButton);
		
		JButton showAllButton = new MyButton("Graphics/Button93-23.png");
		showAllButton.setText("显示所有");
		showAllButton.addActionListener(e->getAll());
		showAllButton.setBounds(430, 35, 93, 23);
		add(showAllButton);

		getAll();
	}
	
	private void getAll(){
		
		try {
			list=bl.get();
			display();
		} catch (RemoteException e1) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e1.getMessage());
			e1.printStackTrace();
		}		
	}
	
	private void select(){
		if(timeFrom.getDate().getTime()>timeTo.getDate().getTime()){
			JOptionPane.showMessageDialog(this, "开始时间不能大于结束时间！");
			return;
		}
		SystemLogVo from=new SystemLogVo();
		SystemLogVo to=new SystemLogVo();
		from.setDate(timeFrom.getDate().getTime());
		to.setDate(timeTo.getDate().getTime()+60*60*24*1000-1);
		try {
			list=bl.get(from, to);
			display();
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
	}

	private void display() {
		Vector<Vector<String>> data=new Vector<Vector<String>>();
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(SystemLogVo o:list){
			Vector<String> item=new Vector<String>();
			item.add(df.format(new Date(o.getDate())));
			item.add(o.getOperator());
			item.add(o.getOperation());
			item.add(o.getObject());
			data.add(item);
		}
		Vector<String> columnNames=new Vector<String>();
		columnNames.add("时间");
		columnNames.add("操作员");
		columnNames.add("操作类型");
		columnNames.add("操作对象");
		table.setModel(new DefaultTableModel(data, columnNames));
	}
}
